<template>
  <div class="order-item">
    <div class="order-item-panel">
      <el-row class="panel-top-label">
        <el-col :span="6">
          <el-checkbox
            :value="orderItem.checked"
            @change="selectItemChange($event, orderItem.id)"
          />
          <span class="ml-10">订单号：{{ orderItem.orderNo }}</span>
        </el-col>
        <el-col :span="6">
          <span>下单时间：{{ orderItem.createdTime }}</span>
        </el-col>
        <el-col :span="6">
          <span style="color: #293c66">下单平台：{{ orderPlatform(orderItem.platform) }}</span>
        </el-col>
        <el-col :span="4" :offset="2" align="center">
          <span>{{ orderType(orderItem.orderSource) }}</span>
        </el-col>
      </el-row>
      <div>
        <div class="panel-top-info ft-12">
          <el-row class="flex items-center">
            <el-col :span="4">
              <span>收货人：</span>
              <span>{{ orderItem.address && orderItem.address.name }}</span>
            </el-col>
            <el-col :span="4">
              <span>联系电话：</span>
              <span>{{ orderItem.address && orderItem.address.mobile }}</span>
            </el-col>
            <el-col :span="8">
              <span>地址：</span>
              <span>{{
                orderItem.address &&
                `${orderItem.address.provinceName}${orderItem.address.cityName}${orderItem.address.districtName}${orderItem.address.detail}`
              }}</span>
              <span
                v-if="
                  orderItem.orderStatus == 10 &&
                  orderItem.address &&
                  orderItem.address.applyStatus == 1
                "
                class="ft-12 ml-10"
                style="color: #fc4541"
                >（客户提交修改地址申请）</span
              >
            </el-col>
            <el-col :span="4"> 配送方式：快递配送</el-col>
            <el-col :span="4" align="center">
              <el-button
                type="text"
                v-if="orderItem.orderStatus === 10"
                style="color: #fc4541"
                @click="shipHandle('add')"
                >立即发货</el-button
              >

              <el-button
                type="text"
                v-if="showShipBtn"
                style="color: #409eff"
                @click="viewLogistics"
                >查看物流</el-button
              >

              <el-button
                type="text"
                class="mt-10"
                v-if="orderItem.mayUpdateAddress && orderItem.orderStatus === 20"
                style="color: #409eff"
                @click="shipHandle('update')"
                >修改物流</el-button
              >

              <el-button type="text" @click="goOrderDetail(orderItem.orderNo)">订单详情</el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="panel-content-box ft-12">
        <el-row class="panel-content" v-for="item in orderItem.detailList" :key="item.goodsSkuId">
          <el-col :span="3">
            <div>
              <div class="flex">
                <el-image
                  class="panel-content-img"
                  :src="item.goodsSkuGraph"
                  :preview-src-list="[item.goodsSkuGraph]"
                />
                <div class="panel-content-text flex flex-col justify-between ml-5">
                  <p class="panel-content_title" style="color: #293c66">{{ item.goodsName }}</p>
                  <p>{{ item.goodsSkuName }}</p>
                </div>
              </div>
              <p v-if="item.goodsSupply" class="mt-10" style="color: #409eff">
                <span>所属供应链：</span>
                <span>{{ item.goodsSupply.name }}</span>
              </p>
            </div>
          </el-col>
          <el-col :span="3" class="text-center">
            <p>{{ item.goodsPrice }}</p>
            <p class="mt-10">¥{{ item.costPrice }}</p>
            <p class="mt-10">{{ item.goodsNum }}</p>
          </el-col>
          <el-col :span="3" class="text-center"> {{ item.supplierName || '暂无' }} </el-col>
          <el-col :span="3" class="text-center">
            <p>
              {{ item.goodsRealityAmount }}积分<span v-if="orderItem.cashAmount != 0"
                >+￥{{ Number(orderItem.cashAmount) + Number(orderItem.freightTotalAmount) }}</span
              >
            </p>
            <p class="mt-10" style="color: red" v-if="Number(orderItem.freightTotalAmount)">
              运费￥{{ orderItem.freightTotalAmount }}
            </p>
            <el-tag v-if="orderItem.integralWarning" class="mt-10" type="danger" effect="dark"
              >预警</el-tag
            >
          </el-col>
          <el-col :span="3" class="text-center">
            <span>{{ payType(item.goodsPriceType) }}</span>
          </el-col>
          <el-col :span="3" class="text-center">
            <order-status :orderItem="orderItem" />
          </el-col>
          <el-col :span="3" class="text-center"> {{ orderItem.userRemark || '暂无' }} </el-col>
          <el-col :span="3" class="text-center"> </el-col>
        </el-row>
      </div>
    </div>

    <!-- 物流发货 -->
    <OrderShipPopup
      :dialogVisible.sync="dialogVisible"
      :row-data="rowData"
      v-if="dialogVisible"
      @update="update"
    />

    <!-- 查看物流 -->
    <OrderLogisticsPopup
      :dialogVisible.sync="dialogExpressVisible"
      :expressIdList="orderItem.expressIdList"
      :detailList="orderItem.detailList"
      :orderNo="orderItem.orderNo"
      :expressType="orderItem.expressType"
      v-if="dialogExpressVisible"
      @update="update"
    />
  </div>
</template>

<script>
import { ORDER_TYPE, ORDER_PLATFORM } from '@/enum/order'
import { mapMutations, mapState } from 'vuex'
import OrderStatus from './OrderStatus.vue'
import OrderShipPopup from './OrderShipPopup.vue'
import OrderLogisticsPopup from './OrderLogisticsPopup.vue'
export default {
  name: 'OrderStoreItem',
  props: {
    orderItem: {
      type: Object,
      default: () => {}
    }
  },
  components: {
    OrderStatus,
    OrderShipPopup,
    OrderLogisticsPopup
  },
  data() {
    return {
      rowData: [],
      dialogVisible: false,
      dialogExpressVisible: false
    }
  },
  computed: {
    ...mapState('order', ['isAll']),
    // 下单平台
    orderPlatform() {
      return data => {
        return ORDER_PLATFORM.find(item => item.value === data)?.label
      }
    },
    // 订单类型
    orderType() {
      return data => {
        return ORDER_TYPE.find(item => item.value === data)?.label
      }
    },
    showShipBtn() {
      // 已发货 待评价 已评价 已完成
      const ids = [20, 30, 31]
      return ids.includes(this.orderItem?.orderStatus)
    },

    // 支付类型
    payType() {
      return function (value) {
        // 1: 积分2: 积分+现金3: 现金
        const obj = {
          1: '积分',
          2: '积分+现金',
          3: '现金'
        }

        return obj[value]
      }
    }
  },
  methods: {
    ...mapMutations('order', ['SELECT_ITEM_CHANGE', 'SELECT_CHECKED_ALL_CHANGE']),
    // 订单详情
    goOrderDetail(orderNo) {
      this.$router.push({ name: 'OrderDetail', query: { orderNo } })
    },
    selectItemChange(checked, id) {
      this.SELECT_ITEM_CHANGE({ key: 'orderList', checked, id })
      this.SELECT_CHECKED_ALL_CHANGE({ key: 'orderList' })
    },
    // 立即发货
    shipHandle(type) {
      this.dialogVisible = true
      this.rowData = {
        type,
        orderNo: this.orderItem.orderNo,
        detailList: this.orderItem?.detailList,
        expressType: this.orderItem.expressType,
        expressIdList: this.orderItem?.expressIdList
      }
    },
    // 查看物流
    viewLogistics() {
      this.dialogExpressVisible = true
    },
    update() {
      this.$emit('update')
    }
  }
}
</script>

<style lang="scss" scoped>
.order-item {
  font-size: 14px;
  margin-top: 15px;
  border: 1px solid #e4e7ed;
  .panel-top-label {
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    padding: 0 20px;
    background-color: $hw-bg-color-grey;
    .operate-Label-item {
      font-weight: bold;
    }
  }
  .panel-top-info {
    padding: 10px 20px;
    background-color: #e2f1f9;
  }
  .panel-content {
    display: flex;
    align-items: center;
    padding: 15px 0px;
    margin: 0 20px;
    font-size: 12px;
    border-bottom: 1px solid #f2f2f2;
    &:last-child {
      border: none;
    }
    &-img {
      width: 50px;
      height: 50px;
    }
    &-text {
      flex: 1;
      cursor: pointer;
      padding: 2px 0;
    }
    .price-num-text {
      padding: 10px 0;
    }
    .lease-text {
      padding: 5px 0;
    }
  }
}
.detal-text {
  color: $hw-text-color;
  cursor: pointer;
}
.text-center {
  text-align: center;
}
.panel-content_title {
  @include ellipsis(2);
  word-break: break-word;
}
</style>
